// pages/dashboard/dashboard.js
// 模拟数据
const mockStocks = [
  { code: '600000', name: '浦发银行', price: 10.25, change: 0.52, open: 10.10, high: 10.30, low: 10.05, volume: 150000 },
  { code: '000001', name: '平安银行', price: 15.30, change: -1.23, open: 15.40, high: 15.45, low: 15.10, volume: 220000 },
  { code: '601318', name: '中国平安', price: 48.75, change: 2.15, open: 48.00, high: 48.90, low: 47.80, volume: 350000 },
  { code: '600519', name: '贵州茅台', price: 1720.50, change: 1.85, open: 1710.00, high: 1725.00, low: 1705.00, volume: 50000 }
];

// 模拟多个策略收益数据
const strategyPerformanceData = {
  'strategy_1': [
    { date: '2023-01-01', value: 0 },
    { date: '2023-01-02', value: 1.2 },
    { date: '2023-01-03', value: 2.5 },
    { date: '2023-01-04', value: 1.8 },
    { date: '2023-01-05', value: 3.2 },
    { date: '2023-01-06', value: 4.1 },
    { date: '2023-01-07', value: 3.7 }
  ],
  'strategy_2': [
    { date: '2023-01-01', value: 0 },
    { date: '2023-01-02', value: 0.8 },
    { date: '2023-01-03', value: 1.5 },
    { date: '2023-01-04', value: 2.1 },
    { date: '2023-01-05', value: 1.9 },
    { date: '2023-01-06', value: 2.6 },
    { date: '2023-01-07', value: 3.2 }
  ],
  'strategy_3': [
    { date: '2023-01-01', value: 0 },
    { date: '2023-01-02', value: -0.5 },
    { date: '2023-01-03', value: 0.2 },
    { date: '2023-01-04', value: 1.1 },
    { date: '2023-01-05', value: 0.8 },
    { date: '2023-01-06', value: 1.4 },
    { date: '2023-01-07', value: 1.9 }
  ]
};

// 用户账户数据
let account = {
  balance: 1000000,
  positions: []
};

Page({
  data: {
    totalAssets: "1,000,000.00",
    availableBalance: "1,000,000.00",
    positionProfit: "0.00",
    positionProfitClass: "text-green-500",
    marketData: [],
    strategyCharts: []
  },

  onLoad() {
    this.updateAccountInfo();
    this.renderMarketTable();
    this.renderStrategyPerformanceChart();
  },

  updateAccountInfo() {
    // 计算总资产（持仓市值 + 可用资金）
    const positionValue = account.positions.reduce((sum, pos) => {
      const stock = mockStocks.find(s => s.code === pos.code);
      return sum + (stock ? stock.price * pos.amount : 0);
    }, 0);

    const totalAssets = account.balance + positionValue;
    
    // 计算持仓收益
    const positionProfit = account.positions.reduce((sum, pos) => {
      const stock = mockStocks.find(s => s.code === pos.code);
      return sum + (stock ? (stock.price - pos.cost) * pos.amount : 0);
    }, 0);

    this.setData({
      availableBalance: account.balance.toLocaleString('zh-CN', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
      }),
      totalAssets: totalAssets.toLocaleString('zh-CN', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
      }),
      positionProfit: positionProfit.toLocaleString('zh-CN', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
      }),
      positionProfitClass: positionProfit >= 0 ? 'text-red-500' : 'text-green-500'
    });
  },

  renderMarketTable() {
    const marketData = mockStocks.map(stock => {
      return {
        code: stock.code,
        name: stock.name,
        price: stock.price.toFixed(2),
        change: stock.change.toFixed(2),
        changeClass: stock.change >= 0 ? 'text-red-500' : 'text-green-500'
      };
    });

    this.setData({
      marketData: marketData
    });
  },

  renderStrategyPerformanceChart() {
    const strategyCharts = Object.keys(strategyPerformanceData).map(strategyId => {
      const data = strategyPerformanceData[strategyId];
      const finalValue = data[data.length - 1].value;
      
      return {
        id: strategyId,
        name: `策略 ${strategyId.split('_')[1]}`,
        finalValue: finalValue.toFixed(2),
        finalValueClass: finalValue >= 0 ? 'text-red-500' : 'text-green-500'
      };
    });

    this.setData({
      strategyCharts: strategyCharts
    });
  },

  onViewStrategyDetail(e) {
    const strategyId = e.currentTarget.dataset.strategy;
    wx.navigateTo({
      url: `/pages/strategy/strategy?tab=detail&id=${strategyId}`
    });
  }
});